//，将该目录下的空目录删除。
//设计一个函数，将指定目录下的空文件删除

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>

void delete(const char *path)
{
    struct stat str;
    struct dirent *dent = NULL;
    DIR *dir = NULL;
    char full_path[256];

    dir = opendir(path);
    if(dir == NULL){
        perror("opendir");
        exit(-1);
    }

    while((dent = readdir(dir)) != NULL){
        if(dent->d_type == DT_REG){
            const size_t full_path_size = sizeof(full_path); // 提前计算并存储大小
if (dent < 0 || (size_t)dent >= full_path_size) {
    // 错误处理：路径生成失败或路径过长
    fprintf(stderr, "Failed to generate full path for: %s/%s\n", path, dent->d_name);
    continue; // 跳过当前项处理
}

            if(stat(full_path, &str) == 0 && str.st_size == 0){
                remove(full_path);
            }
        } else if(dent->d_type == DT_DIR){
            if(strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0){
               const size_t full_path_size = sizeof(full_path); // 提前计算并存储大小
if (ret < 0 || (size_t)dent >= full_path_size) {
    fprintf(stderr, "Failed to generate full path for: %s/%s\n", path, dent->d_name);
    continue; // 跳过当前项处理
}

                delete(full_path);
                rmdir(full_path);
            }
        }
    }

    closedir(dir);
}

int empty(const char *path)
{
    struct dirent *dent = NULL;
    DIR *dir = NULL;
    int ct = 0;

    dir = opendir(path);
    if(dir == NULL){
        perror("opendir");
        exit(-1);
    }

    while((dent = readdir(dir)) != NULL){
        ct++;
    }

    closedir(dir);

    if(ct == 2){
        return 0;
    }
    return -1;
}
